import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { Response } from 'express';
import { WebConfigService } from './modules/web-config/web-config.service';
import { ArticleService } from './modules/article/article.service';
import { Public } from './common/auth/public.decorator';
@Controller()
export class AppController {
  constructor(
    private readonly webConfigService: WebConfigService,
    private readonly articleService: ArticleService,

  ) { }
  @Get()
  @Public()
  async index(@Res() res: Response, @Param() params) {
    const config = await this.webConfigService.getWebConfig()
    const page = await this.articleService.getArticlePage({ status: '1' }, 'page')
    const data = {
      config,
      theme: {},
      page: {
        ...page,
        posts: page.data,
      }
    }
    res.render('index', data);
  }
  @Get('page')
  @Public()
  async page(@Res() res: Response, @Query() params) {
    const config = await this.webConfigService.getWebConfig()
    const page = await this.articleService.getArticlePage({ pageNum: Number(params.pageNum), pageSize: 10 }, 'page')
    const data = {
      config,
      theme: {},
      page: {
        ...page,
        posts: page.data,
      }
    }
    res.render('index', data);
  }
  @Get('categories')
  @Public()
  async categories(@Res() res: Response, @Query() params) {
    const config = await this.webConfigService.getWebConfig()
    const page = await this.articleService.getArticlePage({ categories: params.categories, pageNum: Number(params.pageNum ?? 1), pageSize: 10 }, 'categories')
    const data = {
      config,
      theme: {},
      page: {
        ...page,
        category: params.categories,
        posts: page.data,
      }
    }
    res.render('archive', data);
  }
  @Get('tags')
  @Public()
  async tags(@Res() res: Response, @Query() params) {
    const config = await this.webConfigService.getWebConfig()
    const page = await this.articleService.getArticlePage({ tags: params.tags, pageNum: Number(params.pageNum ?? 1), pageSize: 10 }, 'categories')
    const data = {
      config,
      theme: {},
      page: {
        ...page,
        tag: params.tags,
        posts: {
          each: (callback) => {
            page.data.forEach(callback)
          }
        },
      }
    }
    res.render('archive', data);
  }
  @Get(':title')
  @Public()
  async articleInfo(@Res() res: Response, @Param() params,) {
    const config = await this.webConfigService.getWebConfig()
    const page = await this.articleService.getArticleInfo(params)
    const data = {
      config,
      theme: {},
      page: page.data
    }
    res.render('post', data);
  }

}
